package com.sagframe.sagacity.sqltoy.plus.chain.query;

import com.sagframe.sagacity.sqltoy.plus.chain.ChainDao;
import com.sagframe.sagacity.sqltoy.plus.chain.ChainQuery;
import com.sagframe.sagacity.sqltoy.plus.conditions.AbstractQueryWrapper;
import com.sagframe.sagacity.sqltoy.plus.conditions.segments.MergeSegments;
import org.sagacity.sqltoy.model.Page;

import java.util.List;
import java.util.concurrent.atomic.AtomicInteger;

public abstract class AbstractChainQueryWrapper<T, R, Children extends AbstractChainQueryWrapper<T, R, Children>> extends AbstractQueryWrapper<T, R, Children> implements ChainQuery<T> {

    protected ChainDao chainDao;

    protected AbstractChainQueryWrapper(MergeSegments expression, AtomicInteger paramNameSeq, Class<T> entityClass) {
        super(expression, paramNameSeq, entityClass);
    }

    public AbstractChainQueryWrapper(MergeSegments expression, Class<T> entityClass, ChainDao chainDao) {
        super(expression, entityClass);
        typedThis.chainDao = chainDao;
    }

    @Override
    public List<T> list() {
        return chainDao.getDao().findList(typedThis);
    }

    @Override
    public T one() {
        return chainDao.getDao().findOne(typedThis);
    }

    @Override
    public Page<T> page(Page<?> page) {
        return chainDao.getDao().findPage(typedThis, page);
    }

    @Override
    public long count() {
        return chainDao.getDao().count(typedThis);
    }

    @Override
    public long delete() {
        return chainDao.getDao().delete(typedThis);
    }
}
